package top.sxlai.util.oss;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * @author : sxl
 * @date : 2024/4/7 15:31
 * @Version: 1.0
 */
@Component
public class UploadAvatarFile {
    @Value("${aliyun.oss.endpoint}")
    private  String ALIYUN_OSS_ENDPOINT;
    @Value("${aliyun.oss.accessKeyId}")
    private  String ALIYUN_OSS_ACCESSKEYID;
    @Value("${aliyun.oss.accessKeySecret}")
    private  String ALIYUN_OSS_ACCESSKEYSECRET;
    @Value("${aliyun.oss.bucketName}")
    private  String ALIYUN_OSS_BUCKETNAME;
    @Autowired
    private FileSystem fileSystem;
    //域名
    private static final String DOMAIN = "http://shixulin.com";
    public  String uploadFile(MultipartFile multipartFile) throws IOException {
        OSSClient ossClient = new OSSClient(ALIYUN_OSS_ENDPOINT, ALIYUN_OSS_ACCESSKEYID, ALIYUN_OSS_ACCESSKEYSECRET);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
// 获取文件名
        String fileName = multipartFile.getOriginalFilename();
// 获取文件后缀名
        assert fileName != null;
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
// 最后上传生成的文件名
        String finalFileName = System.currentTimeMillis() + "" + new SecureRandom().nextInt(0x0400) + suffixName;
// oss中的文件夹名
        String objectName = "studyFile/" + sdf.format(new Date()) + "/" + finalFileName;
// 创建上传文件的元信息，可以通过文件元信息设置HTTP header(设置了才能通过返回的链接直接访问)。
//        ObjectMetadata objectMetadata = new ObjectMetadata();
//        objectMetadata.setContentType("image/jpg");
        //文件预览
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setHeader("Content-Disposition", "inline");
// 文件上传
        ossClient.putObject(ALIYUN_OSS_BUCKETNAME, objectName, new ByteArrayInputStream(multipartFile.getBytes()), metadata);
// 设置URL过期时间为10个月。
        Date expiration = new Date(System.currentTimeMillis() + 10L * 30 * 24 * 3600 * 1000);
        //获取url
//        String url = ossClient.generatePresignedUrl(ALIYUN_OSS_BUCKETNAME, objectName, expiration).toString();
        URL signedUrl = ossClient.generatePresignedUrl(ALIYUN_OSS_BUCKETNAME, objectName, expiration);
        String path = signedUrl.getPath();
        String url = DOMAIN + path;
        ossClient.shutdown();
//        filess filess_f = new filess(url, objectName);
        return url;
    }
    public String getUrl(String hdfsPath, int time, String Name) throws IOException {
        OSS ossClient = new OSSClientBuilder().build(ALIYUN_OSS_ENDPOINT, ALIYUN_OSS_ACCESSKEYID, ALIYUN_OSS_ACCESSKEYSECRET);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        // 获取文件名
        String fileName = Name;

        // 获取文件后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));

        // 最后上传生成的文件名
        String finalFileName = System.currentTimeMillis() + "" + new SecureRandom().nextInt(0x0400) + suffixName;

        // oss中的文件夹名
        String objectName = "op_download/" + sdf.format(new Date()) + "/" + finalFileName;

        Path Path = new Path(hdfsPath);
        InputStream inputStream = fileSystem.open(Path);

        // Upload file to OSS
        ossClient.putObject(ALIYUN_OSS_BUCKETNAME, objectName, inputStream);

        // Close InputStream
        inputStream.close();

        // 设置时间
        Date expiration = new Date(System.currentTimeMillis() + time * 30 * 24 * 3600 * 1000L);

        // 生成带有签名的URL
        URL signedUrl = ossClient.generatePresignedUrl(ALIYUN_OSS_BUCKETNAME, objectName, expiration);
        String url = signedUrl.toString(); // 获取完整的URL

        ossClient.shutdown();

        return url;
    }



}
